package com.commerce.controller;

import com.commerce.pojo.dto.Result;
import com.commerce.pojo.po.User;
import com.commerce.service.UserService;
import com.commerce.util.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @PostMapping("save")
    public Result save(@RequestBody User user) {
        Boolean flag = userService.save(user);
        return new Result().ok();
    }

    @PostMapping("login")
    public Result login(@RequestBody User user) {
        User loginUser = userService.login(user);
        String token = JwtUtil.generateToken(loginUser.getId(), loginUser.getUsername());
        Map map = new HashMap();
        map.put("token",token);
        map.put("user",loginUser);
        return new Result(200,"登陆成功",map);
    }

    @GetMapping("getall")
    public Result getall(){
        return new Result(200,"查询所有用户",userService.list());
    }

    @GetMapping("{id}")
    public Result getById(@PathVariable Integer id){
        return new Result(200,"查询单个用户详情",userService.getById(id));
    }
}

